package cn.hzw.supermarket.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.app.StatusBarManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.common.Constants;
import cn.hzw.supermarket.databinding.ActivityWelcomeBinding;
import cn.hzw.supermarket.util.StatusBarUtil;

public class WelcomeActivity extends AppCompatActivity {

    private ActivityWelcomeBinding binding;
    private static final String TAG = "WelcomeActivity";

    //通过参数决定跳转到哪一个页面
    private static final int GO_GUIDE = 1001;       //新手导航页面
    private static final int GO_MAIN = 1002;        //主页面
    private MyCount myCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWelcomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        StatusBarUtil.darkMode(this, ActivityCompat.getColor
                (WelcomeActivity.this,R.color.welcome_statusbar_gray),0);//显示状态栏，设置状态栏 颜色 透明度等参数，默认为黑色
        initView();
        initEvent();
    }

    private void initEvent() {
        binding.guideTimerWrapper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (myCount!=null){
                    myCount.cancel();//点击之后跳过
                    goMain();
                }
            }
        });
    }

    private void initView() {
        final SharedPreferences sp = getSharedPreferences(Constants.SP_CONFIG,MODE_PRIVATE);
        int openCounter = sp.getInt("openCounter",1);//打开次数统计
        SharedPreferences.Editor edit = sp.edit();
        if (openCounter==1){//指定只有下载后第一次跳转导航页
            edit.putInt("openCounter",++openCounter);
            edit.commit();
            handler.sendEmptyMessageDelayed(GO_GUIDE,0);
        }else {
            //否，首页倒计时maxSecond后进入主页面
            edit.putInt("openCounter",++openCounter);
            edit.commit();
            Message message = handler.obtainMessage();
            message.what = GO_MAIN;
            message.arg1 = 1;
            handler.sendMessage(message);
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GO_GUIDE:
                    goGuide();
                    break;
                case GO_MAIN:
                    if (msg.arg1==1){
                        binding.guideTimerWrapper.setVisibility(View.VISIBLE);
                        int maxSecond = Constants.GUIDE_TIMER_MAXSECOND;
                        myCount = new MyCount(maxSecond*1000,1000);//创建一个倒计时 总时长5秒 间隔1秒
                        myCount.start();//开启倒计时
                    }else {
                        goMain();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 跳到引导页
     */
    private void goGuide() {
        Intent intent = new Intent(this,GuideActivity.class);
        startActivity(intent);
        WelcomeActivity.this.finish();
        finish();
    }

    /**
     * 跳到主页面
     */
    private void goMain() {
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 返回键事件
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK&event.getRepeatCount()==0){
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    //上一次按下返回键的时间戳
    protected long mLastBackKeyPressTime;
    //退出逻辑
    protected void exit(){
        if (System.currentTimeMillis()-mLastBackKeyPressTime>1000){
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            mLastBackKeyPressTime = System.currentTimeMillis();
        }else {
            finish();
            System.exit(0);
        }
    }

    //定义一个倒计时的内部类
    class MyCount extends CountDownTimer{
        /**
         *
         * @param millisInFuture 持续时长
         * @param countDownInterval 间隔时长
         */
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        /**
         * 每间隔countDownInterval会调用一次
         * @param millisUntilFinished 已经过去了多长时间
         */
        @Override
        public void onTick(long millisUntilFinished) {
            binding.guideTimer.setText(millisUntilFinished/1000+"");
            Log.i(TAG, "onTick:"+millisUntilFinished/1000);
        }

        /**
         * 在倒计时结束时使用
         */
        @Override
        public void onFinish() {
            binding.guideTimer.setText("0");
            goMain();
        }
    }


}